import { AnimationClip3D, Node3D } from 'feng3d-three';
import { GLTF, GLTFLoader } from '../thirdparty/three/imports';
import { DRACOLoader3D } from './DRACOLoader3D';

export class GLTFLoader3D
{
    _loader: GLTFLoader;

    constructor()
    {
        this._loader = new GLTFLoader();
    }

    setDRACOLoader(dracoLoader: DRACOLoader3D)
    {
        this._loader.setDRACOLoader(dracoLoader._loader);

        return this;
    }

    load(url: string,
        onLoad: (gltf: GLTF3D) => void,
        onProgress?: (event: ProgressEvent) => void,
        onError?: (event: ErrorEvent) => void,
    )
    {
        this._loader.load(url, (gltf) =>
        {
            onLoad(new GLTF3D(gltf));
        }, onProgress, onError);
    }

    async loadAsync(url: string, onProgress?: (event: ProgressEvent) => void)
    {
        const gltf = await this._loader.loadAsync(url, onProgress);
        const gltf3D = new GLTF3D(gltf);

        return gltf3D;
    }
}

export class GLTF3D
{
    scene: Node3D;

    animations: AnimationClip3D[];

    constructor(gltf: GLTF)
    {
        this.scene = Node3D.get(gltf.scene);
        this.animations = gltf.animations.map((v) => AnimationClip3D.get(v));
    }
}
